/**
 * 
 */
package com.yst.metho.web.application.projectversion.actions.wbs;

import java.sql.SQLException;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.Extent;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.event.ActionEvent;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.components.GenericEditWindow;
import com.yst.metho.web.application.components.listingview.ToolbarAction;
import com.yst.metho.web.application.projectversion.views.ActivityPane;
import com.yst.metho.web.application.projectversion.views.ProjectWBSListModel;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.project.versions.lot.Lot;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.status.StatusConstants;

/**
 * @author perso
 * 
 */
public class CreateActivityAction extends ToolbarAction
{
	public static final Extent ACTIVITY_POPUP_WIDTH = new Extent(450, Extent.PX);

	public static final Extent ACTIVITY_HEIGHT = new Extent(260, Extent.PX);

	private ProjectWBSListModel mModel;

	private SelectField mVersionProject;

	private SelectField mLot;

	private Project mPrj;

	/**
	 * 
	 */
	private static final String CREATE_WBS_COMMAND = "CreateActivityFolder";

	public CreateActivityAction(ProjectWBSListModel model, SelectField versionProject, SelectField lot, Project prj)
	{
		super(Right.CREATE_ACTIVITY, "Create");
		mModel = model;
		mPrj = prj;
		mVersionProject = versionProject;
		mVersionProject.addActionListener(this);
		mLot = lot;
		mLot.addActionListener(this);
		setActionCommand(CREATE_WBS_COMMAND);
		addActionListener(this);
		if (StatusConstants.CLOSE == mPrj.getStatus())
		{
			setEnabled(false);
		}
		if (-1 == mLot.getSelectedIndex())
		{
			setEnabled(false);
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		if (CREATE_WBS_COMMAND.equals(e.getActionCommand()))
		{
			// verification qu'une version du projet est selectionne.
			if (-1 != mVersionProject.getSelectedIndex())
			{
				try
				{
					ProjectVersion version = (ProjectVersion) mVersionProject.getSelectedItem();
					Collection folders = Locator.getFolderView().findFolder(mPrj.getId());
					ActivityPane view = new ActivityPane(version, folders, Locator.getTeamView().getTeamRolesFor(version.getProject()), (Lot) mLot.getSelectedItem());
					GenericEditWindow window = new GenericEditWindow(view, "Activite", new ModifyActivityBtn(mModel, view));
					window.setHeight(ACTIVITY_HEIGHT);
					window.setWidth(ACTIVITY_POPUP_WIDTH);
					SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
				}
				catch (FinderException e1)
				{
					ExceptionMgt.manage(e1);
				}
				catch (NamingException e1)
				{
					ExceptionMgt.manage(e1);
				}
				catch (CreateException e1)
				{
					ExceptionMgt.manage(e1);
				}
				catch (SQLException e1)
				{
					ExceptionMgt.manage(e1);
				}
				catch (EJBException e1)
				{
					ExceptionMgt.manage(e1);
				}
			}
			else
			{
				ExceptionMgt.displayMessageBox("Vous devez selectionner une version du projet");
			}
		}
		else if (ProjectWBSListModel.PROJECT_LOT_SELECTION.equals(e.getActionCommand()) || ProjectWBSListModel.PROJECT_VERSION_SELECTION.equals(e.getActionCommand()))
		{
			if (-1 == mLot.getSelectedIndex())
			{
				setEnabled(false);
			}
			else
			{
				setEnabled(true);
			}
			ProjectVersion version = (ProjectVersion) mVersionProject.getSelectedItem();
			if (StatusConstants.CLOSE == version.getStatus())
			{
				setEnabled(false);
			}
			else
			{
				setEnabled(true);
			}
		}
	}
}